package com.redpacketcover.rc.common

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import com.redpacketcover.rc.R
import com.redpacketcover.rc.util.SizeUtils
import kotlinx.android.synthetic.main.view_title_bar.view.*

class TitleBar : RelativeLayout {

    private var title: String? = null
    private var leftSrc: Int = 0
    private var rightSrc: Int = 0
    private var rightText: String? = null
    private var rightTextColor: Int = 0
    private var rightTextSize = 0f
    private var titleTextColor: Int = 0

    private var onFinishListener: OnFinishListener? = null
    private var onRightTextClickListener: OnRightTextClickListener? = null
    private var onRightIconClickListener: OnRightIconClickListener? = null
    private var onLeftIconClickListener: OnLeftIconClickListener? = null

    fun setOnFinishListener(listener: OnFinishListener) {
        onFinishListener = listener
    }

    fun setOnRightTextClickListener(listener: OnRightTextClickListener) {
        onRightTextClickListener = listener
    }

    fun setOnRightIconClickListener(listener: OnRightIconClickListener) {
        onRightIconClickListener = listener
    }

    fun setOnLeftIconClickListner(listener: OnLeftIconClickListener){
        onLeftIconClickListener = listener
    }

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
            context,
            attrs,
            defStyleAttr
    ) {
        init(context, attrs)
    }

    private fun init(context: Context, attrs: AttributeSet?) {
        attrs?.let {
            val a = context.obtainStyledAttributes(attrs, R.styleable.TitleBar)
            title = a.getString(R.styleable.TitleBar_title)
            leftSrc = a.getResourceId(R.styleable.TitleBar_leftSrc, R.mipmap.return_white)
            rightSrc = a.getResourceId(R.styleable.TitleBar_rightSrc, 0)
            rightText = a.getString(R.styleable.TitleBar_rightText)
            rightTextColor =
                    a.getColor(
                            R.styleable.TitleBar_rightTextColor,
                            ContextCompat.getColor(context, R.color.color_999999)
                    )
            rightTextSize =
                    a.getDimension(R.styleable.TitleBar_rightTextSize, SizeUtils.dp2px(14f).toFloat())
            titleTextColor =
                    a.getColor(
                            R.styleable.TitleBar_titleTextColor,
                            ContextCompat.getColor(context, R.color.color_FFFFFF)
                    )
            a.recycle()
        }
        LayoutInflater.from(context).inflate(R.layout.view_title_bar, this, true)
        tb_title.text = title
        tb_title.setTextColor(titleTextColor)
        iv_return.setImageResource(leftSrc)

        if (rightSrc != 0) {
            iv_right.visibility = View.VISIBLE
            iv_right.setImageResource(rightSrc)
        } else {
            iv_right.visibility = View.GONE
        }
        if (!rightText.isNullOrEmpty()) {
            tv_right.visibility = View.VISIBLE
            tv_right.text = rightText
            tv_right.setTextColor(rightTextColor)
            tv_right.setTextSize(TypedValue.COMPLEX_UNIT_PX, rightTextSize)
        } else {
            tv_right.visibility = View.GONE
        }

        iv_return.setOnClickListener {
            if (onFinishListener != null) {
                onFinishListener?.onFinish()
            } else {
                val activity = getActivityFromView(iv_return)
                activity?.finish()
            }

        }
        tv_right.setOnClickListener { onRightTextClickListener?.onRightTextClick() }
        iv_right.setOnClickListener { onRightIconClickListener?.onRightIconClick() }
        iv_return.setOnClickListener { onLeftIconClickListener?.onLeftIconClick() }
    }

    fun setRightTextVisible(visible: Int) {
        tv_right.visibility = visible
    }

    fun setLeftIconVisible(visible: Int) {
        iv_return.visibility = visible
    }

    /**
     * 设置标题文字
     */
    fun setTitle(title: String?) {
        tb_title.text = title
    }

    fun setRightTitle(title: String?){
        tv_right.text = title
    }

    interface OnFinishListener {
        fun onFinish()
    }

    interface OnRightTextClickListener {
        fun onRightTextClick()
    }

    interface OnRightIconClickListener {
        fun onRightIconClick()
    }

    interface OnLeftIconClickListener {
        fun onLeftIconClick()
    }


    fun getActivityFromView(view: View): Activity? {
        var context = view.context
        while (context is ContextWrapper) {
            if (context is Activity) {
                return context
            }
            context = context.baseContext
        }
        return null
    }

}